﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using CustomWindow;
using SteamGroupPlayers.Model;

namespace SteamGroupPlayers.View
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : EssentialWindow
    {
        #region Fields
        private Point startingPoint = new Point(0, 0);
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the MainWindow class.
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();
        }
        #endregion

        #region Methods
        // Helper to search up the VisualTree
        private static T FindAncestor<T>(DependencyObject current)
            where T : DependencyObject
        {
            do
            {
                if (current is T)
                {
                    return (T)current;
                }
                current = VisualTreeHelper.GetParent(current);
            }
            while (current != null);
            return null;
        }

        protected override Decorator GetWindowButtonsPlaceholder()
        {
            return WindowButtonsPlaceholder;
        }

        private void Thumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
        {
            if(Height + e.VerticalChange > winTop.Height)
                Height += e.VerticalChange;
        }

        private void Border_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (e.LeftButton == System.Windows.Input.MouseButtonState.Pressed)
                DragMove();
        }

        private void Border_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (e.ClickCount % 2 == 0)
                if (WindowState == System.Windows.WindowState.Normal)
                    WindowState = System.Windows.WindowState.Maximized;
                else
                    WindowState = System.Windows.WindowState.Normal;
        }

        private void btn_min_Click(object sender, RoutedEventArgs e)
        {
            WindowState = System.Windows.WindowState.Minimized;
        }

        private void btn_max_Click(object sender, RoutedEventArgs e)
        {
            if (WindowState == System.Windows.WindowState.Normal)
                WindowState = System.Windows.WindowState.Maximized;
            else
                WindowState = System.Windows.WindowState.Normal;
        }

        private void btn_close_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void SteamListBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            startingPoint = e.GetPosition(null);
        }

        private void SteamListBox_MouseMove(object sender, MouseEventArgs e)
        {
            Point pos = e.GetPosition(null);
            Vector diff = startingPoint - pos;

            if (e.LeftButton == MouseButtonState.Pressed &&
                (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
                Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))
            {
                // Get the dragged ListViewItem
                ListBox lb = sender as ListBox;
                ListBoxItem lbI =
                    FindAncestor<ListBoxItem>((DependencyObject)e.OriginalSource);

                if (lbI == null)
                    return;

                // Find the data behind the ListViewItem
                var g = lb.ItemContainerGenerator.ItemFromContainer(lbI);
                if (g == null)
                    return;

                Game game = g as Game;
                //Game game = lb.ItemContainerGenerator.ItemFromContainer(lbI) as Game;

                // Initialize the drag & drop operation
                DataObject dragData = new DataObject("steamGameFormat", game);
                DragDrop.DoDragDrop(lbI, dragData, DragDropEffects.Move);
            } 
        }

        private void dropTarget_Drop(object sender, DragEventArgs e)
        {
            Game g = e.Data.GetData("steamGameFormat") as Game;
            (this.Resources["viewModel"] as ViewModel.GroupViewModel).FilterGame = g;
        }

        private void dropTarget_DragEnter(object sender, DragEventArgs e)
        {
            if (!e.Data.GetDataPresent("steamGameFormat") || sender == e.Source)
                e.Effects = DragDropEffects.None;
            else
                e.Effects = DragDropEffects.Copy;
        }

        private void btn_unfilter_Click(object sender, RoutedEventArgs e)
        {
            (this.Resources["viewModel"] as ViewModel.GroupViewModel).FilterGame = null;
        }

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox src = sender as ComboBox;
            if (src.SelectedItem == null)
                return;

            (this.Resources["viewModel"] as ViewModel.GroupViewModel).FilterGame = src.SelectedItem as Game;
        }
        #endregion
    }
}